// 
//  rank.js
//  firefox
//  
//  Created by Seed on 2008-10-23.
//  Copyright 2008 Ant.com. All rights reserved.
// 


var AntRank = 
{
    rank: null,
    url: null,
    cacheRank: null,
    init: function()
    {
        this.cacheRank = new AntArray();
    },

    isValidRankUrl: function (url)
    {
      var ios = CCSV('@mozilla.org/network/io-service;1', "nsIIOService");
      try
      {
        var uri = ios.newURI(url, null, null);
        return (url.match(/^https?:\/\//i) && uri.host != "localhost");
      }
      catch (e)
      { 
        AntLib.toLog("error isValidRankUrl : " + e);
        return false; 
      }
    },

    cleanRank: function ()
    {
        this.url = null;
        this.rank = null;
        var lab = AntLang.getString("AntRank.noRank"); 
        this.setLabelRank(lab);
    },

    setLabelRank: function (rank)
    {
        $("antToolBarRankButton").setAttribute("label", rank);
        var size = 20 + 12 * rank.length;
        if (size < 60)
                size = 60;
        $("antToolBarRankButton").setAttribute("width", size);
        $("antToolBarRankButton").setAttribute("minwidth", size);
    },

    updateRank: function (aURI)
    {
      this.cleanRank();
      this.url = aURI.spec;
      if (this.isValidRankUrl(this.url))
      {
        var domain = AntLib.getDomain(this.url);
        if (this.cacheRank[domain])
           this.rank = this.cacheRank[domain];
        else
           this.rank = AntLib.fetchURL("http://rank.ant.com/fetch/" + AntLib.uriEncode(this.url));
        this.cacheRank[domain] = this.rank;
      }
      else
        this.rank = null;
      if (!this.rank || !parseInt(this.rank, 10))
        this.rank = AntLang.getString("AntRank.noRank"); 

      this.rank = this.rank.trim();
      this.setLabelRank(this.rank);
    },

    goProfil: function ()
    {
       var domain = AntLib.getDomain(this.url);
        if (domain)
        {
          profileUrl = "http://ant.com/rank/" + domain;
          gBrowser.selectedTab = gBrowser.addTab(profileUrl);
        }
    }
};

    
